package com.kaizzi.data.common.result;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.kaizzi.data.common.constant.ResultCodeEnum;
import lombok.Data;

/**
 * @author Kzen
 * @Description description
 * @date 2020/9/8
 */
@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Result<T> {

    private Integer code;

    private String msg;

    private T data;


    public Result() {
    }


    public Result(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }


    public Result(Integer code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }


    /**
     * 请求成功
     *
     * @param msg 返回信息
     * @return Result
     */
    public static Result getSuccess(String msg) {
        return new Result(ResultCodeEnum.SUCCESS.getCode(), msg);
    }


    /**
     * 请求成功
     *
     * @param msg  返回信息
     * @param data 返回对象
     * @param <T>  类型
     * @return Result
     */
    @SuppressWarnings("unchecked")
    public static <T> Result getSuccess(String msg, T data) {
        return new Result(ResultCodeEnum.SUCCESS.getCode(), msg, data);
    }


    /**
     * 请求失败
     *
     * @param msg 返回信息
     * @return Result
     */
    public static Result getFailed(String msg) {
        return new Result(ResultCodeEnum.FAILED.getCode(), msg);
    }


    /**
     * 请求失败
     *
     * @param msg  返回信息
     * @param data 返回数据
     * @param <T>  类型
     * @return Result
     */
    @SuppressWarnings("unchecked")
    public static <T> Result getFailed(String msg, T data) {
        return new Result(ResultCodeEnum.FAILED.getCode(), msg, data);
    }


    /**
     * 用户未登录
     *
     * @return Result
     */
    public static Result getNoLogin() {
        return new Result(ResultCodeEnum.UNAUTHORIZED.getCode(), ResultCodeEnum.UNAUTHORIZED.getMsg());
    }


    /**
     * 用户没有操作权限
     *
     * @return Result
     */
    public static Result getNoPrivilege() {
        return new Result(ResultCodeEnum.NO_PRIVILEGE.getCode(), ResultCodeEnum.NO_PRIVILEGE.getMsg());
    }


}
